import React from 'react'
import {
  BaseListPage,
  IBaseListPageProps,
  IBaseListPageState,
  http,
  UdModal,
  uaaApp,
} from '@/lib'
import { columnRenders } from '../../../lib/utils/columnRenders'
import apis from '@/apis/UserManagement'
import {
  message,
  Button,
  Modal,
} from 'antd'
import {
  getBrandSelectList
} from '@/common/Request'
import AddStaffArea from './AddStaffArea'

const { confirm } = Modal;

const addBtnAuth = 'user_lzlj_employee_manager_staff_area_add'
const updateBtnAuth = 'user_lzlj_employee_manager_staff_area_update'
const deleteBtnAuth = 'user_lzlj_employee_manager_staff_area_delete'

export default class List extends BaseListPage<IProductMasterDataProps, IProductMasterDataState> {

  formRef: any = React.createRef();

  constructor(props) {
    super(props)
    this.state = {
      title: '内部员工授权管理区域管理',
      queryApi: (data) => http.post(apis.internalStaffList, {
        page: data.page,
        size: data.size,
        conditions: data.userCode || data.realName ? {
          userCode: data.userCode,
          realName: data.realName,
        } : undefined,
      }),
      method: 'POST',
      rowKey: 'id',
      conditions: [
        { id: 'userCode', label: '员工编码' },
        { id: 'realName', label: '员工姓名' },
      ],
      columns: [
        ['员工编码', 'userCode'],
        ['员工姓名', 'realName'],
        ['所属品牌公司', 'brandName'],
        ['授权管理区域', 'districtName'],
        ['创建时间', 'createTime'],
        ['创建人', 'createUser'],
        ['最近修改时间', 'updateDate'],
        ['最近修改人', 'updateUser'],
        {
          title: '操作',
          dataIndex: 'isValid',
          render: columnRenders.operate([
            {
              title: '编辑', auth: updateBtnAuth, action: (text, record) => this.openEdit(record)
            },
            {
              title: '移除', auth: deleteBtnAuth, action: (text, record) => this.deleteArea(record.id)
            },
          ])
        }
      ],
      leftBtns: [
        uaaApp.canUse(addBtnAuth) && <Button type='primary' onClick={this.addStaffArea}>新增</Button>,
      ],
      brandList: [],
      treeData: [],
      loading: false,
      customerCodes: [],
    }
  }

  public componentDidMount() {
    super.componentDidMount()
    this.getBrandSelectList()
  }

  addStaffArea = () => {
    const {
      brandList,
    } = this.state;

    UdModal.open({
      width: "600px",
      title: "内部员工授权管理区域",
      content: (
        <AddStaffArea brandList={brandList} />
      ),
      onOk: async (v) => {
        const { data }: any = await http.post(apis.getStaffList, { userCode: v.userCode })

        const params = {
          ...v,
          realName: data[0].realName,
          districtCode: v.districtCode.join(',')
        }

        http.post(apis.addStaffArea, params).then(({ data }: any) => {
          if (!data) return message.warning('当前用户已存在区域授权信息，请勿重复添加!')
          message.success('添加信息成功')
          this.query()
        })
      }
    })
  }

  deleteArea = (id: any) => {
    this.pushQueryParams()
    confirm({
      title: '确定移除吗？',
      onOk: () => {
        http.post(apis.deleteInternalStaffArea, {id}).then((data: any) => {
          message.success('移除成功')
          this.query()
        })
      },
    });
  }

  getBrandSelectList = async () => {
    this.setState({
      brandList: await getBrandSelectList() as any[]
    })
  }


  openEdit = (info: any) => {
    const {
      brandList,
    } = this.state;

    UdModal.open({
      width: "600px",
      title: "内部员工授权管理区域",
      content: (
        <AddStaffArea
          ref={this.formRef}
          brandList={brandList}
          info={info}
        />
      ),
      onOk: async (v) => {
        const { data }: any = await http.post(apis.getStaffList, { userCode: v.userCode })

        const params = {
          ...info,
          ...v,
          realName: data[0].realName,
          districtCode: v.districtCode.join(',')
        }

        http.post(apis.updateStaffArea, params).then(({ data }: any) => {
          message.success('更新信息成功')
          this.query()
        })
      }
    })

    setTimeout(() => {
      this.formRef.current.initData()
    })
  }
}

interface IProductMasterDataProps extends IBaseListPageProps {
}

interface IProductMasterDataState extends IBaseListPageState {
  brandList: any[],
  treeData: any[],
  loading: Boolean,
  customerCodes: any[],
}
